package com.mall.order.services;

import com.alibaba.druid.support.profile.ProfileEntryReqStat;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.order.OrderUpdateService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.OrderUpdateResponse;
import com.mall.order.dto.OrderUpdateReuqest;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xudengfeng
 * @date 2020/12/23 14:15
 */
@Service(interfaceClass = OrderUpdateService.class)
@Component
public class OrderUpdateServiceImpl implements OrderUpdateService {

    @Autowired
    OrderMapper orderMapper;


    // 根据request中的status改变订单状态
    @Override
    public OrderUpdateResponse updateStatusByOrderId(OrderUpdateReuqest orderUpdateReuqest) {

        OrderUpdateResponse resp = new OrderUpdateResponse();

        try {
            orderUpdateReuqest.requestCheck();

            String orderId = orderUpdateReuqest.getOrderId();
            Integer status = orderUpdateReuqest.getStatus();

            Order order = new Order();
            order.setOrderId(orderId);
            order.setStatus(status);

            Integer rows = orderMapper.updateByPrimaryKeySelective(order);
            if (rows != null) {
                resp.setCode(OrderRetCode.SUCCESS.getCode());
                resp.setMsg(OrderRetCode.SUCCESS.getMessage());
            }
        } catch (ValidateException validateException) {
            resp.setCode(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode());
            resp.setMsg(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        } catch (Exception e) {
            resp.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
            resp.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
        }
        return resp;
    }
}
